package org.hk.proxy.server.container;

import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.hk.proxy.common.container.Container;
import org.hk.proxy.common.container.ContainerHelper;
import org.hk.proxy.server.utils.CommonUtils;
import org.hk.proxy.server.utils.SpringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.Arrays;

@Configuration
@Slf4j
public class AppStartUpContainer implements ApplicationListener<ContextRefreshedEvent> {
    public static ApplicationContext context;
    private  void start(){
        CommonUtils.initConfig();
        ContainerHelper.start(Arrays.asList(new Container[] { new ProxyServerContainer() ,new WebServerContainer(),new ProxySocket5Container()}));
    }
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null) {
            SpringUtils.context=event.getApplicationContext();
            ThreadUtil.execute(()->start());
        }
    }
}
